package cn.edu.neu.lab603.cloudeval.monitor

import cn.edu.neu.lab603.des.SimMetric

/** 获取 Fitness 的指标。
  *
  * Created by yfwz100 on 2016/11/17.
  */
class FitnessScoreMetric(val migrationDurationMonitor: MigrationDurationMonitor,
                         val migrationDownTimeMonitor: MigrationDownTimeMonitor,
                         val cloudletSLAMonitor: CloudletSLAMetric,
                         val powerMetric: PowerMetric) extends SimMetric[Double] {

  /** 获取当前的监控数值。 */
  override def getCurrentValue: Double = {
    val avgDownTime = if (migrationDownTimeMonitor.stats.nonEmpty)
      migrationDownTimeMonitor.stats.values.map(_.sum).sum / migrationDownTimeMonitor.stats.size
    else 0
    val avgMigrationTime = if (migrationDurationMonitor.stats.nonEmpty)
      migrationDurationMonitor.stats.values.map(_.sum).sum / migrationDurationMonitor.stats.size
    else 0
    val migrationCost = 0.6 * avgDownTime + 0.4 * avgMigrationTime
    val avgSLA = cloudletSLAMonitor.averageSLAMetric.getCurrentValue
    val power = powerMetric.getCurrentAccumulatedPower / 3600000.0
    power * avgSLA * migrationCost
  }
}
